package com.github.itdachen.utils;

import com.github.itdachen.entity.DevTableInfo;

/**
 * Description: 模板工具类
 * Created by 王大宸 on 2022-07-27 23:45
 * Created with IntelliJ IDEA.
 */
public class VelocityUtils {

    /***
     * 访问地址
     *
     * @author 王大宸
     * @date 2023/9/23 23:48
     * @param tableInfo tableInfo
     * @return java.lang.String
     */
    public static String getAskUri(DevTableInfo tableInfo) {
        String contextPath = contextPathHandler(tableInfo.getContextPath());
        if (null == contextPath) {
            contextPath = "";
        }
        String rearUri = tableInfo.getRearUri();
        if (null != rearUri && !rearUri.isEmpty()) {
            rearUri = getControllerUri(tableInfo);
        }
        if (null == rearUri) {
            return "";
        }
        tableInfo.setRearUri(rearUri.toLowerCase());
        if (contextPath.isEmpty()) {
            return rearUri.toLowerCase();
        }
        String askUri = "";
        if (null != tableInfo.getModuleName() && !"".equals(tableInfo.getModuleName())) {
            askUri = contextPath + "/" + tableInfo.getModuleName() + "/" + rearUri;
        } else {
            askUri = contextPath + "/" + rearUri;
        }
        return askUri.toLowerCase().replace("//", "/");
    }

    /***
     * controller 映射路径
     *
     * @author 王大宸
     * @date 2023/9/23 23:47
     * @param tableInfo tableInfo
     * @return java.lang.String
     */
    public static String getControllerUri(DevTableInfo tableInfo) {
        return "/" + tableInfo.getTableName().replaceAll("_", "/");
    }


    private static String contextPathHandler(String contextPath) {
        if (null == contextPath || "".equals(contextPath)) {
            return "";
        }
        if (contextPath.startsWith("/")) {
            return contextPath;
        }
        return "/" + contextPath;
    }

}
